<?php
require_once("../../class2.php");
require_once(HEADERF);
		$user_id = USERID;
		$user_name = USERNAME;
require_once("./includes/require_all.php");
function spaceATtree($var){ return number_format($var, 0, ",", " "); }
$inners=array("3pacalypse Now","HeBuguM","Mnementh","xakepa","benzinjiq","moonface","Zlo","purpleisdead","mitaka18","e-VIL","aXe","tsetse","kockata1","LawFall","moga","TuRoKF");
ramka("open");

$games_data= array();
$tempdata= mysql_query("SELECT id,game_name,game_prefix,urlprefix,PRIVATEGAME FROM admin_panel");
WHILE($t=mysql_fetch_array($tempdata)){
	$gd_id[]= $t['id'];
	$gd_fullname[]= $t['game_name'];
	$gd_urlpref[]= $t['urlprefix'];
	$gd_gamepref[]= $t['game_prefix'];
	$gd_private[]= $t['PRIVATEGAME'];
}

echo "<a href='http://playonbg.info/translations/' class='forumheader4' style='font-size:14px; margin-left:10px;'>НАЗАД</a><br>";
echo "<center class='forumheader4 unselectable defcursor' style='margin-top:-20px; margin-left:-1px; padding-bottom:6px;'><h1 style='border-bottom:0px;'>Статистика</center></h1>";
CMS_activitySave("stats.php");

$load= mysql_query("SELECT * FROM cms_statisticTWO where game != 'AssCreed2' ORDER BY game ASC");

while ($row= mysql_fetch_array($load)) {
	if(false!=$key=array_search($row['game'],$gd_urlpref)) {}
	elseif(false!=$key=array_search($row['game'],$gd_gamepref)) {}
	else $key=false;
	if($key!=false) {
		if($gd_private[$key]==1) {
			$row['game']= "Необявен превод ".$gd_id[$key];
		} else 
			$row['game']= $gd_fullname[$key];
	} else 	$row['game']= $gd_fullname[$key];
	$all_games++; $all_files+= (int)$row['files']; $all_strings+= (int)$row['strings']; $all_translated+= (int)$row['translated'];
	if ($row['percent']=="100") $all_finished++;
} unset ($load);

$all_strings= spaceATtree($all_strings);$all_translated_temp= spaceATtree($all_translated);
$primestats= mysql_fetch_assoc(mysql_query("SELECT SUM(wordsall) as wordsall,SUM(wordsdone) as wordsdone,SUM(charall) as charall,SUM(charbg) as charbg FROM admin_panel"));
$wordsall= spaceATtree($primestats['wordsall']);$wordsdone= spaceATtree($primestats['wordsdone']);
$charall= spaceATtree($primestats['charall']);$charbg= spaceATtree($primestats['charbg']);
$mpages_all= ceil($primestats['charall']/1800); $mpages_bg= ceil($primestats['charbg']/1800);
$aslevas= ceil($mpages*7.5);

echo <<<HTML
			<div style="border: 0px solid; border-color: grey; font-family:Georgia,Serif; ">
			<div style="border-bottom:0px solid grey; padding:6px; font-size:135%" class="forumheader"><b class='dictWHITE unselectable defcursor'>Обща информация</b></div>
			<table cellpadding='5' CELLSPACING='0' CELLPADDING ='4' WIDTH='100%' class="forumheader3 unselectable defcursor">
				<tr style="font-weight:bold;">
					<td WIDTH=10%></td>
					<td WIDTH=15%></td>
					<td WIDTH=10%>Игри</td>
					<td WIDTH=15%>Файлове</td>
					<td WIDTH=15%>Реплики</td>
					<td WIDTH=15%>Думи</td>
					<td WIDTH=15%>Знаци</td>
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td style="font-weight:bold;">Всичко</td>
					<td class="dictYELLOW" style="font-size:125%; border-bottom:0px dotted grey;">$all_games</td>
					<td class="dictYELLOW" style="font-size:125%; border-bottom:0px dotted grey;">$all_files</td>
					<td class="dictYELLOW" style="font-size:125%; border-bottom:0px dotted grey;">$all_strings</td>
					<td class="dictYELLOW" style="font-size:125%; border-bottom:0px dotted grey;">$wordsall</td>
					<td class="dictYELLOW" style="font-size:125%; border-bottom:0px dotted grey;">$charall</td>					
					<td></td>
				</tr>
				<tr>
					<td></td>
					<td style="font-weight:bold;">Преведени</td>
					<td class="dictGREEN" style="font-size:135%;">$all_finished</td>
					<td class="dictGREEN" style="font-size:135%;"></td>
					<td class="dictGREEN" style="font-size:135%">$all_translated_temp</td>
					<td class="dictGREEN" style="font-size:135%">$wordsdone</td>
					<td class="dictGREEN" style="font-size:135%">$charbg</td>
					<td></td>
				</tr>		
			</table>
			<div class='forumheader'>В системата се съдържа текст, равняващ се на <span class='textYELLOW'>$mpages_all</span> машинописни страници; <span class='textGREEN'>$mpages_bg</span> стр. е българският превод.</div> 
			</div>
HTML;

echo "<div class='forumheader4'><center class='textBLACK'>--</center></div>";

$load= mysql_query("SELECT * FROM cms_statisticONE where game != 'AssCreed2' ORDER BY date DESC, game ASC, username ASC");
while ($row= mysql_fetch_array($load)) {
	if(false!=$key=array_search($row['game'],$gd_urlpref)) {}
	elseif(false!=$key=array_search($row['game'],$gd_gamepref)) {}
	else $key=false;
	if($key!=false) {
		if($gd_private[$key]==1) {
			$row['game']= "Необявен превод ".$gd_id[$key];
		} else 
			$row['game']= $gd_fullname[$key];
	} else 	$row['game']= $gd_fullname[$key];
	$user[]= $row['username']; $game[]= $row['game']; $count[]= (int)$row['strings']; $date[]= $row['date'];
} unset ($load);

$today= date("Y-m-d");
foreach ($date as $key=>$d) {
	if ($d==$today) {
			$today_overal+=$count[$key];
			$today_uoveral[$user[$key]]+=$count[$key];
			$today_goveral[$game[$key]]+=$count[$key];
	}
	else break;
}

$weekdays= date("N"); $w1=date("Y");$w2=date("m");$w3=date("d"); 
$weekstart= date('Y-m-d', mktime(0,0,0,$w2, ($w3-$weekdays), $w1));
$week_overal=0; $week_uoveral=array();
foreach ($date as $key=>$d) {
	if ($d>$weekstart) {
			$week_overal+=$count[$key];
			$week_uoveral[$user[$key]]+=$count[$key];
			$week_goveral[$game[$key]]+=$count[$key];			
	}
	else break;
}

$monthstart= date("Y-m-01"); $month_overal=0; $month_uoveral=array();
foreach ($date as $key=>$d) {
	if ($d>$monthstart) {
			$month_overal+=$count[$key];
			$month_uoveral[$user[$key]]+=$count[$key];
			$month_goveral[$game[$key]]+=$count[$key];
	}
	else break;
}

function print_sections($overal,$uoveral,$goveral,$period){
	if ($overal === null) $overal = 0;
	
	echo <<<HTML
				<div style="border: 0px solid; border-color: grey;">
				<div style="border-bottom:0px solid grey; padding:6px; font-family:Georgia,Serif; font-size:135%; font-weight:normal;" class="forumheader textGREY unselectable defcursor"><b class='dictWHITE'>$period</b> са били преведени: <b class='dictWHITE'>$overal</b> реплики</div>
				<table cellpadding='5' WIDTH='100%' class='unselectable defcursor'>
HTML;
	
	if (count($uoveral)<count($goveral)) {
		$toadd= count($goveral)-count($uoveral);
		for($i=1; $i<=$toadd; $i++) {
			array_push($uoveral, "||~~>>$i");
		}
	}
	$g_keys= array_keys($goveral); $g_vals= array_values($goveral); $num=0;
	foreach ($uoveral as $user => $count) {
		if($num%2==1) {$class= "cmsRow1";} else $class= "cmsRow2";
		$icon1='<img src="icons/user.png" WIDTH=100% HEGHT=100% align="right" title="Преводач" style="cursor:help;"/>';
		$icon2='<img src="icons/cd.png" WIDTH=100% HEGHT=100% align="right" title="Игра" style="cursor:help;"/>';
		if(strpos("x".$count,'||~~>>') !== false) { $user=""; $count=""; $icon1="";}
		if(isset($g_keys[$num])) { $game= $g_keys[$num]; $count2= $g_vals[$num]; }
		else { $game=""; $count2=""; $icon2=""; }
		echo <<<HTML
					<tr class="forumheader3 bluehover" STYLE="font-size:14px;">
					<td WIDTH=3%>$icon1</td>
					<td class="textYELLOW" WIDTH=27%>$user</td>
					<td WIDTH=20%>$count</td>
					<td WIDTH=3% style="border-left:0px solid grey;">$icon2</td>
					<td class='textGREEN' WIDTH=27%>$game</td>
					<td WIDTH=20%>$count2</td>
					</tr>
HTML;
		$num++;
	}
	echo "</table></div>";
}
 
arsort($today_uoveral);arsort($week_uoveral);arsort($month_uoveral);
arsort($today_goveral);arsort($week_goveral);arsort($month_goveral);
print_sections($today_overal,$today_uoveral,$today_goveral,"Днес");
print_sections($week_overal,$week_uoveral,$week_goveral,"Тази седмица");
print_sections($month_overal,$month_uoveral,$month_goveral,"Този месец");
echo "<div style='border:0px solid grey; color:red; padding:6px; font-family:Georgia,Serif; font-size:120%; font-weight:normal;' class='forumheader'>Тази статистика се изчислява от 02:00ч. на 04.12.2011г.</div>";

function rank_check($translated) {
	if ($translated>=15000) { $ico= "✔"; $styl='GREEN'; $tito= 'Гуру'; }
	elseif ($translated>=10000) { $ico= "✔"; $styl='YELLOW'; $tito= 'Майстор'; }
	elseif ($translated>=7500) { $ico= "✔"; $styl='RED'; $tito= 'Преводач'; }
	elseif ($translated>=5000) { $ico= "✉"; $styl='GREEN'; $tito= 'Доказан инат'; }
	elseif ($translated>=2500) { $ico= "✉"; $styl='YELLOW'; $tito= 'Превел една игра'; }
	elseif ($translated>=1000) { $ico= "✉"; $styl='RED'; $tito= 'Напредващ'; }
	elseif ($translated>=500) { $ico= "✖"; $styl='GREEN'; $tito= 'Начинаещ преводач'; }
	elseif ($translated>=100) { $ico= "✖"; $styl='YELLOW'; $tito= 'Новак'; }
	elseif ($translated>=1) { $ico= "✖"; $styl='RED'; $tito= 'Свободен елемент'; }
	$ico= "<span class='text$styl' style='cursor:help;' title='$tito'>$ico</span>";
	return $ico;
}

$load= mysql_query("SELECT user_name, approved_rows, approved_unseen, machinni FROM cms_actusers WHERE approved_rows != '0' OR approved_unseen != '0'");
$translators= mysql_num_rows($load);
while ($row= mysql_fetch_array($load)) {
	if(in_array($row['user_name'],$inners)){
		$teammember[$row['user_name']]=(int)$row['approved_rows']+(int)$row['approved_unseen']; }
	else $translator[$row['user_name']]=(int)$row['approved_rows']+(int)$row['approved_unseen'];
	$machinni[$row['user_name']]=$row['machinni'];
} unset($load);
$teammember["Mnementh"]+=$teammember["LawFall"]; unset($teammember["LawFall"]);
$machinni["Mnementh"]+=$machinni["LawFall"]; unset($machinni["LawFall"]);
arsort($translator); arsort($teammember); $translators_did= array_sum($translator); $team_did= array_sum($teammember); $translators_didnot= $all_translated-($translators_did+$team_did); $unique_trans=$translators_did+$team_did; 
$fake[0]= ""; $allmachinni=0; foreach($translator as $user=>$count){ $fake[]= $user; $allmachinni+=$machinni[$user]; }
$num=1; $trans_rows= ceil(count($translator)/3);

echo <<<HTML
			<div class='forumheader4'><center class='textBLACK'>--</center></div>
			<div style="border-bottom:0px solid grey; padding:6px; font-size:135%; font-family:Georgia,Serif; font-weight:normal; margin-bottom:2px;" class="forumheader textGREY unselectable defcursor">Уникални преведени реплики: <b class='dictWHITE'>$unique_trans</b></div>
HTML;

echo <<<HTML
			<div style="border: 0px solid; border-color: grey;">
			<div style="border-bottom:0px solid grey; padding:6px; font-size:135%; font-family:Georgia,Serif; font-weight:normal;" class="forumheader textGREY unselectable defcursor"> ➡ <b class='dictWHITE'> Класация на потребителите </b> (Общо: <b class='dictWHITE'>$translators_did</b> реплики, <b class='dictWHITE'>$allmachinni</b> маш.стр.)</div>
			<table cellpadding='5' CELLSPACING='0' CELLPADDING ='4' WIDTH='100%' class='unselectable defcursor'>
HTML;
foreach ($translator as $user => $count) {
	if($num%2==0) {$class= "cmsRow1";} else $class= "cmsRow2";
	$num2= $num+$trans_rows;  $user2= $fake[$num2]; $count2= $translator[$user2];
	$num3= $num2+$trans_rows; $user3= $fake[$num3]; $count3= $translator[$user3];
	$ico= rank_check($count);
	if ($num2>$translators) {$num2=null; $ico2=null;} else $ico2= rank_check($count2);
	if ($num3>$translators) {$num3=null; $ico3=null;} else $ico3= rank_check($count3);
	if(isset ($machinni[$user])) { $mpages= "($machinni[$user] <span style='font-size:10px;'>стр.</span>)";} else $mpages="";
	if(isset ($machinni[$user2])) { $mpages2= "($machinni[$user2] <span style='font-size:10px;'>стр.</span>)";} else $mpages2="";
	if(isset ($machinni[$user3])) { $mpages3= "($machinni[$user3] <span style='font-size:10px;'>стр.</span>)";} else $mpages3="";
	echo <<<HTML
				<tr class="forumheader3 bluehover" style='font-size:14px'>
				<td WIDTH=2% align='center'>$ico</td>
				<td class="textYELLOW" WIDTH=10%>$user</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count $mpages</td>
				<td WIDTH=2% align='center'>$ico2</td>
				<td class="textYELLOW" WIDTH=10%>$user2</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count2 $mpages2</td>
				<td WIDTH=2% align='center'>$ico3</td>
				<td class="textYELLOW" WIDTH=10%>$user3</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count3 $mpages3</td>
				</tr>
HTML;
	$num++; if($num>$trans_rows) break;
}
echo "</table></div>";
//------------------------------------------------------------------------------
$fake=array(); $fake[0]= ""; $allmachinni=0; foreach($teammember as $user=>$count){$fake[]= $user; $allmachinni+=$machinni[$user];}
$num=1; $trans_rows= ceil(count($teammember)/3);

echo <<<HTML
			<div style="border: 0px solid; border-color: grey;">
			<div style="border-bottom:0px solid grey; padding:6px; font-size:135%; font-family:Georgia,Serif; font-weight:normal;" class="forumheader textGREY unselectable defcursor"> ➡ <b class='dictWHITE'> Класация на екипа </b> (Общо: <b class='dictWHITE'>$team_did</b> реплики, <b class='dictWHITE'>$allmachinni</b> маш.стр.)</div>
			<table cellpadding='5' CELLSPACING='0' CELLPADDING ='4' WIDTH='100%' class='unselectable defcursor'>
HTML;
foreach ($teammember as $user => $count) {
	$num2= $num+$trans_rows;  $user2= $fake[$num2]; $count2= $teammember[$user2];
	$num3= $num2+$trans_rows; $user3= $fake[$num3]; $count3= $teammember[$user3];
	$ico= rank_check($count);
	if ($num2>$translators) {$num2=null; $ico2=null;} else $ico2= rank_check($count2);
	if ($num3>$translators) {$num3=null; $ico3=null;} else $ico3= rank_check($count3);
	if(isset ($machinni[$user])) { $mpages= "($machinni[$user] <span style='font-size:10px;'>стр.</span>)";} else $mpages="";
	if(isset ($machinni[$user2])) { $mpages2= "($machinni[$user2] <span style='font-size:10px;'>стр.</span>)";} else $mpages2="";
	if(isset ($machinni[$user3])) { $mpages3= "($machinni[$user3] <span style='font-size:10px;'>стр.</span>)";} else $mpages3="";
	echo <<<HTML
				<tr class="forumheader3 bluehover" style='font-size:14px'>
				<td WIDTH=2% align='center'>$ico</td>
				<td class="textYELLOW" WIDTH=10%>$user</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count $mpages</td>
				<td WIDTH=2% align='center'>$ico2</td>
				<td class="textYELLOW" WIDTH=10%>$user2</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count2 $mpages2</td>
				<td WIDTH=2% align='center'>$ico3</td>
				<td class="textYELLOW" WIDTH=10%>$user3</td>
				<td WIDTH=13% style="border-right:0px solid #040404;">$count3 $mpages3</td>
				</tr>
HTML;
	$num++; if($num>$trans_rows) break;
}
echo "</table></div>";


echo "<div style='border:0px solid grey; color:red; padding:6px; font-family:Georgia,Serif; font-size:120%; font-weight:normal;' class='forumheader'>Тази статистика не включва $translators_didnot реплики - повтарящи се, преведени преди вкарването им в системата или отпреди статистиката.</div>";

ramka("close");

require_once(FOOTERF);
?>